<?php
namespace service;

use think\Exception;
use think\exception\ValidateException;

class UploadService
{
    private static $imageValidate = ['size'=>2097152,'ext'=>'jpg,jpeg,png,gif','mime'=>'image/jpeg,image/gif,image/png'];

    public static function uploadImage($input_name, $dir = 'uploads' )
    {
        $file = request()->file($input_name);
        if($file == null)
            return [
                'code' => 0,
                'msg' => '上传文件有误',
            ];
        $info = $file->validate(self::$imageValidate)->move($dir);
        if($info){
            $file_info = $info->getinfo();
            return [
                'code' => 1,
                'msg' => '已上传',
                'name' => $info->getFilename(),
                'size' => $file_info['size'],
                'ext' => $info->getExtension(),
                'path' => '/' . $dir . '/' . str_replace('\\','/',$info->getSaveName()),
            ];
//            dump($file_info);
//            dump($info->getExtension());
//            dump($info->getSaveName());
//            dump($info->getFilename());
//            dump([
//                $file_info['name'],
//                $file_info['size'],
//                $file_info['type'],
//                $dir,
//                '', // 压缩后的地址
//                $pid
//            ]);
        }else{
            // 上传失败获取错误信息
            return [
                'code' => 0,
                'msg' => $file->getError(),
            ];
        }
    }
    public static function uploadImageBase64($base64_image_content, $dir = 'uploads' ){
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result) == false)
            return [
                "code" => "0",
                "msg" => "文件内容有误",
            ];

        //图片后缀
        $type = $result[2];
        if($type=='jpeg') $type='jpg';
        //保存位置--图片名
        $image_name= date('ymdHis').str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).".".$type;
        $image_url = '/' . $dir . '/'.date('Ymd').'/'. $image_name;
        if(!is_dir(dirname('.'.$image_url))){
            mkdir(dirname('.'.$image_url));
            chmod(dirname('.'.$image_url), 0777);
        }
        //解码
        $decode = base64_decode(str_replace($result[1], '', $base64_image_content));
        if (file_put_contents('.'.$image_url, $decode)){
            $img_len = strlen($base64_image_content);
            return [
                'code' => 1,
                'msg' => '已上传',
                'name' => $image_name,
                'size' => $img_len - ($img_len / 8) * 2,
                'ext' => $type,
                'path' => $image_url,
            ];
        }else{
            return [
                "code" => "0",
                "msg" => "上传失败",
            ];
        }

    }
}